// EmulateLCDSegmentsDoc.cpp : implementation of the CEmulateLCDSegmentsDoc class
//

#include "stdafx.h"
#include "EmulateLCDSegments.h"

#include "EmulateLCDSegmentsDoc.h"
#include ".\emulatelcdsegmentsdoc.h"
#include "SegmentsDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CEmulateLCDSegmentsDoc

IMPLEMENT_DYNCREATE(CEmulateLCDSegmentsDoc, CDocument)

BEGIN_MESSAGE_MAP(CEmulateLCDSegmentsDoc, CDocument)
	//ON_COMMAND(ID_SEGMENTS_INPUT, OnSegmentsInput)
	ON_UPDATE_COMMAND_UI(ID_SEGMENTS_INPUT, OnUpdateSegmentsInput)
END_MESSAGE_MAP()


// CEmulateLCDSegmentsDoc construction/destruction

CEmulateLCDSegmentsDoc::CEmulateLCDSegmentsDoc()
: CodeDisplaySimbol(0)
, m_LCD_CHARS(_T(""))
{
	// TODO: add one-time construction code here

}

CEmulateLCDSegmentsDoc::~CEmulateLCDSegmentsDoc()
{
}

BOOL CEmulateLCDSegmentsDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}




// CEmulateLCDSegmentsDoc serialization

void CEmulateLCDSegmentsDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}


// CEmulateLCDSegmentsDoc diagnostics

#ifdef _DEBUG
void CEmulateLCDSegmentsDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CEmulateLCDSegmentsDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG


// CEmulateLCDSegmentsDoc commands

void CEmulateLCDSegmentsDoc::OnSegmentsInput()
{
	// TODO: Add your command handler code here
	CSegmentsDlg MyDlg;
	MyDlg.m_Segs_Chars=m_LCD_CHARS;
	if (MyDlg.DoModal()==IDOK)
	{
		size_t l=(size_t)(MyDlg.m_Segs_Chars.GetLength()+1);
		m_LCD_CHARS=MyDlg.m_Segs_Chars;
		char*tmp=(char*)malloc(l);
		memset(tmp,0,l);
		l--;
		for (size_t LoopIndex=0;LoopIndex<l;LoopIndex++)
		{
			tmp[LoopIndex]=MyDlg.m_Segs_Chars.GetAt((int)LoopIndex);
		}
		CodeDisplaySimbol=GetCodeDispSimbFromSegsChars(tmp);
		free(tmp);
		tmp=NULL;
	}
}

void CEmulateLCDSegmentsDoc::OnUpdateSegmentsInput(CCmdUI *pCmdUI)
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(true);
}
signed int CEmulateLCDSegmentsDoc::GetBitNumberFromSegmentNumber (unsigned int SegmentNumber)
	 {
	     signed int BitNums[0x10]={0,4,8,12,
		                           10,6,9,5,
								   1,7,3,11,
								   15,13,2,14};
		 if (!(SegmentNumber>>4))
		 {
		     return BitNums[SegmentNumber];
	     }
		 else
		 {
		     return -1;
		 }
	     
     }
signed int CEmulateLCDSegmentsDoc::GetBitNumberFromSegmentChar (char SegmentChar)
	 {
	     unsigned int tmp=((unsigned int)SegmentChar)&0x1F;
		 return GetBitNumberFromSegmentNumber(--tmp);
	 }
short int CEmulateLCDSegmentsDoc::GetCodeDispSimbFromSegsChars (char * SegmentsChars)
	 {
	     int LoopIndex=0;
		 short int CodeDisplaySimbol=0;
	     while (SegmentsChars[LoopIndex])
		 {
		     short int tmp=GetBitNumberFromSegmentChar (SegmentsChars[LoopIndex]);
			 if (tmp>=0)
			 {
			     CodeDisplaySimbol|=1<<(tmp);
			 }
		     LoopIndex++;
		 }
		 return CodeDisplaySimbol;
     }
